package cn.maydaymemory.mastery;

import cn.maydaymemory.mastery.command.CommandMastery;
import cn.maydaymemory.mastery.command.CommandMasteryAdmin;
import cn.maydaymemory.mastery.command.api.CommandManager;
import cn.maydaymemory.mastery.command.api.SuperCommand;
import cn.maydaymemory.mastery.configuration.ConfigurationManager;
import cn.maydaymemory.mastery.configuration.LanguageManager;
import cn.maydaymemory.mastery.listener.ShopInventoryListener;
import cn.maydaymemory.mastery.shop.Shop;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class PluginMastery extends JavaPlugin {
    private ConfigurationManager configurationManager;
    private LanguageManager lang;
    private YamlConfiguration current_lang;
    private static Economy econ = null;

    private static PluginMastery instance;   {instance = this;}

    @Override
    public void onEnable(){
        version= Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];

        configurationManager = new ConfigurationManager(this);
        lang = new LanguageManager(this);

        reload();

        SuperCommand commandMasteryAdmin = new SuperCommand("MasteryAdmin",new CommandMasteryAdmin(),current_lang);
        List<String> aliases = new ArrayList<>();
        aliases.add("ma");
        commandMasteryAdmin.setAliases(aliases);
        commandMasteryAdmin.executor().helperEnable(false);
        CommandManager.register(commandMasteryAdmin);

        SuperCommand commandMastery = new SuperCommand("Mastery",new CommandMastery(),current_lang);
        List<String> aliases2 = new ArrayList<>();
        aliases2.add("m");
        commandMastery.setAliases(aliases2);
        CommandManager.register(commandMastery);

        Bukkit.getPluginManager().registerEvents(new ShopInventoryListener(),this);
    }

    public void reload(){
        File config = new File(getDataFolder(),"config.yml");
        File goods  = new File(getDataFolder(),"goods.yml");
        configurationManager.reload(config);
        configurationManager.reload(goods);

        String langTag = configurationManager.getConfig(config).getString("lang","default");
        if(langTag!=null) {
            if (langTag.equalsIgnoreCase("default")){
                lang.preload();
                current_lang = lang.getLang();
            }
            else {
                lang.preload(langTag);
                current_lang = lang.getLang(langTag);
            }
        }

        if(configurationManager.getConfig(config).getBoolean("vault",false)){
            setupEconomy();
        }

        Shop.refreshCurrency();
        Shop.refreshGoods();
    }

    private void setupEconomy() {
        if (getServer().getPluginManager().getPlugin("Vault") == null) {
            return;
        }
        RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
        if (rsp == null) {
            return;
        }
        econ = rsp.getProvider();
    }

    public Economy getEcon(){return econ;}

    public static PluginMastery getInstance(){return instance;}

    public LanguageManager getLanguage(){  return lang;  }

    public ConfigurationManager getConfigManager(){    return configurationManager;   }

    public Configuration getCurrent_lang(){  return current_lang;   }



    private String version;

    public Class getNmsClass(String name) throws ClassNotFoundException {
        return Class.forName("net.minecraft.server." + version + "." + name);
    }

    public Class getCbClass(String name) throws ClassNotFoundException {
        return Class.forName("org.bukkit.craftbukkit." + version + "." + name);
    }

    @Override
    public void onDisable(){
        File goods  = new File(getDataFolder(),"goods.yml");
        configurationManager.save(goods);
    }
}
